package br.cns;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import br.cns.experiments.ComplexNetwork;
import br.cns.models.ErdosRenyiM;
import br.cns.models.TModel;
import br.cns.persistence.ComplexNetworkDaoXml;

public class ComplexNetworkDaoXmlTest {
	@Test
	public void testSave() {
		boolean pass = true;
		int nodes = 20;

		double[][] positions = new double[nodes][2];

		for (int i = 0; i < positions.length; i++) {
			positions[i][0] = Math.random() * (500 - 20);
			positions[i][1] = Math.random() * (500 - 40);
		}

		int[][] adjacencyMatrix = new int[nodes][nodes];

		ErdosRenyiM erdos = new ErdosRenyiM(0.1, nodes);
		adjacencyMatrix = erdos.transform(adjacencyMatrix);

		ComplexNetwork rede = new ComplexNetwork(0, adjacencyMatrix, positions,
				TModel.ERDOS_RENYI_N_M);
		try {
			ComplexNetworkDaoXml.getInstance().save("C:\\Temp\\rede.xml", rede);
		} catch (IOException e) {
			pass =  false;
		}
		if (!new File("C:\\Temp\\rede.xml").exists()){
			pass =  false;
		}
		assertEquals(pass, true);
	}

	@Test
	public void testLoad() {
		boolean pass = true;
		try {
			ComplexNetwork loadedNet = ComplexNetworkDaoXml.getInstance().load(
					"C:\\Temp\\rede.xml");
			if (!(loadedNet.getModel().equals(TModel.ERDOS_RENYI_N_M)
					&& loadedNet.getAdjacencyMatrix().length == 20 && loadedNet
						.getNodePositions().length == 20)) {
				pass = false;
			}
		} catch (IOException e) {
			pass = false;
		}
		assertEquals(pass, true);
	}

}
